package com.example.demo.thread.thread;

import java.util.Random;

/**
 * 线程终止与可见性
 *
 * @author: htc
 * @date: Created in 16:23 2019/1/7.
 */
public class ThreadJoinVisibility {
    /**
     * 线程间的共享变量
     */
    static int data = 0;

    public static void main(String[] args) {
        Thread thread = new Thread() {
            @Override
            public void run() {
                //使当前线程休眠R秒(R为随机数)
                try {
                    Thread.sleep(new Random().nextInt(50));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                //更新data值
                data = 1;
            }
        };

        thread.start();

        //等待线程thread结束后，main线程才继续运行
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //读取并打印变量data的值
        System.out.println(data);
    }
}
